import { createRouter, createWebHistory } from 'vue-router'

//路由数组
const routes = [
    {
        path: "/",
        redirect:"/index"
    },
    {
        path: "/login",
        name: "login",
        component: () => import("./page/Login.vue")
    },
    {
        path: "/post",
        name: "post",
        component: () => import("./page/department.vue")
    },
    {
        path: "/index",
        name: "index",
        component: () => import("./page/Index.vue"),
        children:[
            {
                path: "",
                redirect:"/index/user"
            },
            {
                path: "post",
                name: "post",
                component: () => import("./page/post.vue")
            },
            {
                path: "department",
                name: "department",
                component: () => import("./page/department.vue")
            },
            {
                path: "post",
                name: "post",
                component: () => import("./page/post.vue")
            },
            {
                path: "user",
                name: "user",
                component: () => import("./page/UserPage.vue")
            },
            {
                path: "role",
                name: "role",
                component: () => import("./page/RolePage.vue")
            },
            {
                path: "teacher",
                name: "teacher",
                component: () => import("./page/TeacherPage.vue")
            },
            {
                path: "class",
                name: "class",
                component: () => import("./page/ClassPage.vue")
            },
            {
                path: "course",
                name: "course",
                component: () => import("./page/CoursePage.vue")
            }
        ]
    },
]

//路由对象
const router = createRouter({
    history: createWebHistory(),
    routes //上面的路由数组

})

router.beforeEach((to,from,next)=>{
    if(to.path === '/login'){
        next()
    }
    let userUuid = sessionStorage.getItem("userUuid")
    if(userUuid){
        next()
    }
    next('/login')
});

//导出路由对象，在main.js中引用
export default router
